
import { createRouter, createWebHistory } from 'vue-router'
// import { useRouter } from 'vue-router'
import useUserStore from '../store/user'
// 引入路由表
import routes from './routes'


// console.log(import.meta.env.PROD);
const router = createRouter({
    history: createWebHistory(import.meta.env.PROD ? '/' : '/dev'),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // return 期望滚动到哪个的位置
        return { top: 0 }
    }
})


router.beforeEach((to, from, next) => {
    if(to.meta.Auth) {
        if(useUserStore().token) {
            next()
        }else {
            ElMessage.error('未登录,请登录后再次访问!')
            next('/login')
        }
    }else {
        next()
    }
})


export default router